package BackEnd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import pac.saveScreen;

public class Folio implements IFolio {
	private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
	private JFrame frame;
	LinkedList<Stock> folio = new LinkedList<Stock>();
	Map<String, Integer> foliomap = new HashMap<String, Integer>();
	ArrayList<String> stockName = new ArrayList<String>();
	ArrayList<Double> stockPrice = new ArrayList<Double>();
	ArrayList<Integer> stockNum = new ArrayList<Integer>();
	ArrayList<String> saveNames = new ArrayList<String>();
	String sName = null;

	public Map<String, Integer> getFoliomap() {
		return foliomap;
	}

	int folioSize;

	public Folio() {
		folioSize = 0;
	}

	@Override
	public void buyShares(String stockTicker, int numShares, double costPerStock) {

		double newPrice;
		int newNum;

		if (!foliomap.containsKey(stockTicker.toUpperCase()))
			addNewStock(stockTicker.toUpperCase(), numShares, costPerStock);
		else {
			// check bounds
			// TODO
			newPrice = stockPrice.get(foliomap.get(stockTicker)) + costPerStock;
			newNum = stockNum.get(foliomap.get(stockTicker)) + numShares;
			stockPrice.set(foliomap.get(stockTicker), newPrice);
			stockNum.set(foliomap.get(stockTicker), newNum);
			folio.get(foliomap.get(stockTicker)).setCostPerStock(costPerStock);
			folio.get(foliomap.get(stockTicker)).buyShares(numShares);
		}
	}


	public void save(){

		try{

			sName = JOptionPane.showInputDialog(null,
					  "Enter file name");
			sName = sName + ".txt";

            String fileName = sName;
            
			for(int i = 0; i < folioSize; i++){
				PrintWriter out1 = new PrintWriter(new FileWriter(fileName, true));
				out1.println(stockName.get(i) + " " + stockNum.get(i) + " " + stockPrice.get(i));
				out1.close();

			}


		}catch (IOException e2){
			System.out.println("IOException: Please Retry");
		}	
	}
	

	@Override
	public void addStock(String stockTicker) {
		double costPerStock = 0;
		int numShares = 0;
		if (!foliomap.containsKey(stockTicker.toUpperCase())){
			addNewStock(stockTicker.toUpperCase(), numShares, costPerStock);
		}else {
			// check bounds
			// TODO
			folio.get(foliomap.get(stockTicker)).buyShares(numShares);
		}
	}

	private void addNewStock(String stockTicker, int numShares,
			double costPerStock) {
		foliomap.put(stockTicker, folioSize++);
		folio.add(new Stock(numShares, costPerStock));
		stockName.add(stockTicker.toUpperCase());
		stockPrice.add(numShares * costPerStock);
		stockNum.add(numShares);
	}

	@Override
	public int getNumShares(String stockTicker) {
		return folio.get(foliomap.get(stockTicker)).getNumShares();
	}

	@Override
	public double getPotentialSaleValue(String stockTicker) {
		return folio.get(foliomap.get(stockTicker)).getPotentialSaleValue();
	}

	@Override
	public double getStockNetValue(String stockTicker) {
		return folio.get(foliomap.get(stockTicker)).getNetValue();
	}

	@Override
	public double getTotalCost(String stockTicker) {
		return folio.get(foliomap.get(stockTicker)).getTotalCost();
	}

	@Override
	public void removeStock(String stockTicker) {
		folio.get(foliomap.get(stockTicker)).clear();
		folio.remove(foliomap.get(stockTicker));
		System.out.println("removed " + stockTicker);
	}

	@Override
	public double sellShares(String stockTicker, int numShares) {
		// Check bounds
		// TODO
		return folio.get(foliomap.get(stockTicker)).sellShares(numShares);
	}

	@Override
	public void setCostPerShare(String stockTicker, double costPerStock) {
		folio.get(foliomap.get(stockTicker)).setCostPerStock(costPerStock);
	}
}
